package com.ktjy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.WebApplicationContext;

@Controller
public class ScopeController {
    // 注入 WebApplicationContext 可以获得容器
    @Autowired
    private WebApplicationContext context;

    @GetMapping("/scope")
    public String testScope() {
        System.out.println("转发前的输出");
        printBeanInfo();
        // 转发到/forward 控制器再打印一次
        return "forward:/forward";
    }

    @GetMapping("/forward")
    public String forward() {
        System.out.println("转发后的输出");
        printBeanInfo();
        return null;
    }

    private void printBeanInfo() {
        Object user = context.getBean("user");
        System.out.println("user= " + user);
    }
}